<?php

require_once('inc/addons.php');
require_once('inc/users.php');
require_once('inc/functions.php');

$ratings = array(
	'-' => 'Unrated',
	'0' => '0 - Awful',
	'1' => '1',
	'2' => '2 - Bad',
	'3' => '3',
	'4' => '4 - Poor',
	'5' => '5',
	'6' => '6 - Reasonable',
	'7' => '7',
	'8' => '8 - Good',
	'9' => '9',
	'10' => '10 - Excellent'
);

function formatRating($rating) {
	$d = round($rating,0);
	for ($i = 0; $i < $d; $i++) {
		$o .= '<img src="/res/star-filled.png" alt="Filled star" style="vertical-align: middle;">';
	}
	for ($i = $d; $i < 10; $i++) {
		$o .= '<img src="/res/star-unfilled.png" alt="Unfilled star" style="vertical-align: middle;">';
	}
	$o .= ' (' . $rating . ')';
	return $o;
}

Users::startSession();

$sql  = 'SELECT addons.addon_id, addon_title, user_displayname AS addon_author, addon_verified, ';
$sql .= 'addon_description, addon_type, addon_date, version_version, version_download, ';
$sql .= 'addon_screenshot, addon_rating, addon_core, version_id FROM addons LEFT OUTER JOIN ';
$sql .= 'versions ON versions.addon_id = addons.addon_id NATURAL JOIN users ';
$sql .= 'WHERE version_stable = 1 AND addons.addon_id = ' . ((int) $_GET['id']) ;
$res  = mysql_query($sql) or print(mysql_error());

if (mysql_num_rows($res) == 0) {
	require('404.html');
	exit;
}

$row  = mysql_fetch_assoc($res);

if (isset($_POST['rating']) && Users::isLoggedIn() && isset($ratings[$_POST['rating']])) {
	Users::getUser()->setRating($_GET['id'], $_POST['rating']);
	header('Location: /addon/' . $_GET['id']);
	exit;
} else if (isset($_POST['comment']) && !empty($_POST['comment']) && Users::isLoggedIn()) {
	Users::getUser()->addComment($_GET['id'], $_POST['comment']);
	header('Location: /addon/' . $_GET['id']);
	exit;
}

showHeader('Addon Information :: '.htmlentities($row['addon_title']));

if ($row['addon_date'] > 0) {
	$date = date('F j, Y', $row['addon_date']);
	$days = round((time() - $row['addon_date']) / (60 * 60 * 24), 0);

	$date .= ' (' . $days . ' day' . ($days == 1 ? '' : 's') . ' ago)';
} else {
	$date = 'Unknown';
}

echo '<h2>', htmlentities($row['addon_title']);
if (!empty($row['version_version'])) { echo ' version ', htmlentities($row['version_version']); }
echo '</h2>';

if ($row['addon_core'] == 'yes') {
	echo '<div style="border: 1px solid blue; background-color: #ccf; padding: 5px; margin-bottom: 20px;">';
	echo ' This is a <em>core addon</em> &mdash; it was written by the DMDirc developers and is included ';
	echo ' with official DMDirc distributions.';
	echo '</div>';
} else if ($row['addon_verified'] != 'yes') {
	echo '<div style="border: 1px solid red; background-color: #fcc; padding: 5px; margin-bottom: 20px;">';
	echo ' This addon has not been verified. This means that it hasn\'t yet been inspected';
	echo ' by a DMDirc developer or volunteer.';
	echo '</div>';
}

echo '<div style="float: right; width: 250px;">';
echo ' <div style="height: 250px; width: 250px; border: 1px solid #666; text-align: center;">';
if (empty($row['addon_screenshot'])) {
	echo '<img src="http://www.dmdirc.com/res/logo.png" alt="DMDirc logo" style="margin-top: 50px;">';
	echo '<p style="text-align: center;">No screenshot available</p>';
} else {
	echo '<img src="/addonimg/'.$row['addon_id'].'" alt="Screenshot of '.htmlentities($row['addon_title']).'">';
}
echo ' </div>';
echo ' <div style="border: 1px solid #666; border-top: 0px; padding: 10px 0px; text-align: center; background-color: #77CC77; width: 250px;">';

if (empty($row['version_download'])) {
	echo 'No download available';
} else {
	echo '<a href="/addondownload/'.$row['version_id'].'" style="color: #228822;">Download now!</a>';
}

echo ' </div>';
echo '</div>';

echo '<div style="margin-right: 270px;">';
echo '<p>Author: <em>', htmlentities($row['addon_author']), '</em>;';
echo '<span style="padding: 0px 10px;">Submitted on: <em>';
echo $date, '</em>;</span> Rating: ', formatRating($row['addon_rating']), '</p>';

echo '<h3>Author\'s description</h3>';
echo '<p>', nl2br(htmlentities($row['addon_description'])), '</p>';
echo '<h3>Comments</h3>';

$sql  = 'SELECT user_name, comment_time, comment_comment FROM comments NATURAL ';
$sql .= 'JOIN users WHERE addon_id = ' . ((int) $_GET['id']) . ' ORDER BY ';
$sql .= 'comment_time DESC';
$res  = mysql_query($sql);

if (mysql_num_rows($res) == 0) {
	echo '<p style="font-style: italic;">There have been no comments about this addon yet.</p>';
} else {
	while ($comment = mysql_fetch_assoc($res)) {
		echo '<div class="comment">';
		echo '<h4>Comment by ', htmlentities($comment['user_name']), '</h4>';
		echo '<p>', htmlentities($comment['comment_comment']), '</p>';
		echo '</div>';
	}
}


echo '<h3>Your opinion</h3>';

if (Users::isLoggedIn()) {
	$myrating = Users::getUser()->getRating($_GET['id']);

	echo '<h4>Rating</h4>';

	echo '<form action="/addon/', htmlentities($_GET['id']), '" method="post">';
	echo ' <select name="rating">';

	foreach ($ratings as $rating => $description) {
		echo '  <option value="', htmlentities($rating), '"';

		if ($rating == '-') {
			echo ' style="font-style: italic;"';
		}

		if (strcmp($rating, $myrating) == 0) {
			echo ' selected="selected"';
		}

		echo '>', htmlentities($description), '</option>';
	}

	echo ' </select>';
	echo ' <input type="submit" value="Update">';
	echo '</form>';

	echo '<h4>Add Comment</h4>';

	echo '<form action="/addon/', htmlentities($_GET['id']), '" method="post">';
	echo ' <textarea name="comment" rows="10" cols="80"></textarea>';
	echo ' <input type="submit" value="Add">';
	echo '</form>';
} else {
	echo '<p style="font-style: italic;">You must be <a href="/login">logged in</a> to comment on and rate addons.</p>';
}

echo '</div>';

showFooter();
?>
